#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Date    : 2016/10/28 14:48
# @Author  : xycfree
# @Link    : http://example.org
# @Version : $

import os
import remote,conf,db

class Online(object):
	def __init__(self):
		self.query = '''SELECT a.projectName, a.path, b.ip
			 FROM online a, cmdb b WHERE a.serverName=b.application
		'''
		self.project_info = db.db_select_all(self.query) #获取所有项目信息
		print self.project_info

	def start(self,project_name):
		self.query = '''SELECT a.projectName,a.path,b.ip FROM online a, cmdb b
							WHERE a.serverName=b.application and a.projectName="{project_name}"
							'''.format(project_name=project_name)
		try:
			self.project_name,self.path,self.ip = db.db_select_on(self.query)
			#self.project = os.path.join(self.path,self.project_name)
			r = remote.Remote()
			command = 'cd {path}/{project_name};git pull'.format(path=self.path,project_name=self.project_name)
			print command
			result = r.run(ip=self.ip,command=command)
			return result
		except Exception, e:
			print str(e)

if __name__ == '__main__':
	o = Online()
	s = o.start('AOps')
	print s,s['message']
